/**
 * Created with PyCharm.
 * User: xiaowei
 * Date: 14-8-20
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("esri.map");
dojo.require("esri.layers.agstiled");
dojo.require("esri.toolbars.draw");
dojo.require("dijit.dijit"); // optimize: load dijit layer
dojo.require("esri.undoManager");
dojo.provide("myModules.customoperation");
var mymap,toolbar,reExtent,boundary_List,undoManager,resizeTimer;

//撤销 重做 申明
dojo.declare("myModules.customoperation.Add", esri.OperationBase, {
    label: "Add Graphic",
    constructor: function ( /*graphicsLayer, addedGraphic*/ params) {
        params = params || {};
        if (!params.graphicsLayer) {
            console.error("graphicsLayer is not provided");
            return;
        }
        this.graphicsLayer = params.graphicsLayer;

        if (!params.addedGraphic) {
            console.error("no graphics provided");
            return;
        }
        this._addedGraphic = params.addedGraphic;
    },

    performUndo: function () {
        this.graphicsLayer.remove(this._addedGraphic);
    },

    performRedo: function () {
        this.graphicsLayer.add(this._addedGraphic);
    }
});
//初始化地图
function init(){
    undoManager = new esri.UndoManager();
    mymap = new esri.Map("map");
    //加载天地图
    dojo.declare("TianDiTiledMapServiceLayer", esri.layers.TiledMapServiceLayer, {
        constructor: function() {
            this.spatialReference = new esri.SpatialReference({ wkid:4326 });
            this.initialExtent = (this.fullExtent =
                new esri.geometry.Extent(-180.0, -90.0, 180.0, 90.0, this.spatialReference));
            this.tileInfo = new esri.layers.TileInfo({
                "rows" : 256,
                "cols" : 256,
                "compressionQuality" : 0,
                "origin" : { "x" : -180,"y" : 90},
                "spatialReference" : {"wkid" : 4326},
                "lods" : [
                    {"level" : 0, "resolution" : 0.703125, "scale" : 295497593.05875},
                    {"level" : 1, "resolution" : 0.3515625, "scale" : 147748796.529375},
                    {"level" : 2, "resolution" : 0.17578125, "scale" : 73874398.264688},
                    {"level" : 3, "resolution" : 0.087890625, "scale" : 36937199.132344},
                    {"level" : 4, "resolution" : 0.0439453125, "scale" : 18468599.566172},
                    {"level" : 5, "resolution" : 0.02197265625, "scale" : 9234299.783086},
                    {"level" : 6, "resolution" : 0.010986328125, "scale" : 4617149.891543},
                    {"level" : 7, "resolution" : 0.0054931640625, "scale" : 2308574.945771},
                    {"level" : 8, "resolution" : 0.00274658203125, "scale" : 1154287.472886},
                    {"level" : 9, "resolution" : 0.001373291015625, "scale" : 577143.736443},
                    {"level" : 10, "resolution" : 0.0006866455078125, "scale" : 288571.86822143558},
                    {"level" : 11, "resolution" : 0.00034332275390625, "scale" : 144285.93411071779},
                    {"level" : 12, "resolution" : 0.000171661376953125, "scale" : 72142.967055358895},
                    {"level" : 13, "resolution" : 8.58306884765625e-005, "scale" : 36071.483527679447},
                    {"level" : 14, "resolution" : 4.291534423828125e-005, "scale" : 18035.741763839724},
                    {"level" : 15, "resolution" : 2.1457672119140625e-005, "scale" : 9017.8708819198619},
                    {"level" : 16, "resolution" : 1.0728836059570313e-005, "scale" : 4508.9354409599309},
                    {"level" : 17, "resolution" : 5.3644180297851563e-006, "scale" : 2254.4677204799655}]
            });
            this.loaded = true;
            this.onLoad(this);
        },
        getTileUrl: function(level, row, col) {
            if(level<10){
                levelMap = "A0512_EMap";
            }else if(level<12){
                levelMap = "B0627_EMap1112";
            }else if(level<18){
                levelMap = "siwei0608";
            }
            return "http://t1.tianditu.cn/DataServer?T=vec_c&"+levelMap+"&X=" + col + "&Y=" + row + "&L=" + (level*1+1);
        }

    });

    //文字标注
    dojo.declare("TianDiBiaoZhuMapServiceLayer", TianDiTiledMapServiceLayer, {
        getTileUrl: function(level, row, col) {
            return "http://t1.tianditu.cn/DataServer?T=cva_c&X=" + col + "&Y=" + row + "&L=" + (level*1+1);
        }
    });
    //影像地图
    dojo.declare("TianDiYXMapServiceLayer", TianDiTiledMapServiceLayer, {
        getTileUrl: function(level, row, col) {//wmts
            return "http://t1.tianditu.cn/DataServer?T=img_c&X=" + col + "&Y=" + row + "&L=" + (level*1+1);
        }
    });

    //加载天地图结束

    dojo.connect(mymap, 'onLoad', function(theMap) {
        //resize the map when the browser resizes
        dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
    });

    dojo.connect(mymap, "onLoad", createToolbar);
    dojo.connect(mymap, "onLoad", function(){
        loadParkRange();
        loadEntersRange();
    });
    dojo.connect(mymap, "onExtentChange", showExtent);
    //configure map pan animation to be slower
    esri.config.defaults.map.panDuration = 1000; //time in milliseconds; default panDuration:250
    esri.config.defaults.map.panRate = 50; //refresh rate of zoom animation; default panRate:25
    mymap.addLayer(new TianDiTiledMapServiceLayer());
    mymap.addLayer(new TianDiYXMapServiceLayer());
    mymap.addLayer(new TianDiBiaoZhuMapServiceLayer());

    mymap.resize();//
    mymap.reposition();   //复位
    var point = $("#rangePoint");

    // reExtent = new esri.geometry.Extent({"xmin":118.754667,"ymin":32.170577,"xmax":118.98104,"ymax":32.219344,"spatialReference":{"wkid":102100}});
    // reExtent = new esri.geometry.Extent({"xmin":118.3,"ymin":31.170577,"xmax":119.3,"ymax":32.7,"spatialReference":{"wkid":102100}});
    // if(geo_center_point)
    //     reExtent = new esri.geometry.Extent({"xmin":geo_center_point[0]-0.05,"ymin":geo_center_point[1]-0.05,"xmax":geo_center_point[0]+0.05,"ymax":geo_center_point[1]+0.05,"spatialReference":{"wkid":102100}});
    
    if(p_extent)
        reExtent = new esri.geometry.Extent({
            "xmin":p_extent.XMin,
            "ymin":p_extent.YMin,
            "xmax":p_extent.XMax,
            "ymax":p_extent.YMax,
            "spatialReference":{
                "wkid":54032
            }
        });
        
    mymap.setExtent(reExtent);//区域范围
}

dojo.addOnLoad(init);//调用init


//init();
function createToolbar(themap) {
    //resize the map when the browser resizes
    dojo.connect(dijit.byId('map'), 'resize', mymap,mymap.resize);
    // toolbar = new esri.toolbars.Draw(mymap);
    // dojo.connect(toolbar, "onDrawEnd", addToMap);
}

var dotList=new Array();

function toSubmitModal(title,content,type){
    $('#Modal').modal({show:true});
    $('#Modal #content').empty().append(content);
    $("#Modal #title").empty().append(title);
    $("#submit").on('click',function(){
        submitWaterSource();
    });
}

function showExtent(extent) {

}
// init ...................

function loadEntersRange(){
    if(!es) return false;

    var solidColor = [254,236,36],
        fillColor = [255, 52, 179,255],
        fill_color = [255,255,255,0];



    for(var i=0; i<es.length; i++){

        var rangePolygon = {
            "attributes":{
                "name": es[i].id,
                "extent": es[i].extent,
            },
            "geometry":{
                "rings":[ es[i].points ],
                "spatialReference": { "wkid": 102100 }
            },
            "symbol":{
                "color":fill_color,
                "outline":{
                    "color":fillColor,
                    "width":2,
                    "type":"esriSLS",
                    "style":"esriSLSSolid"
                },
                "type":"esriSFS",
                "style":"esriSFSSolid"
            }
        };
        
        if(es[i].points.length!=0){

            var gra = new esri.Graphic(rangePolygon);
            var entCenterPoint=gra.geometry.getExtent().getCenter();
            var font = new esri.symbol.Font("14px", esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL, esri.symbol.Font.WEIGHT_BOLDER);
            var textSymbol = new esri.symbol.TextSymbol(es[i].name, font, new dojo.Color([255, 255, 0]));
            var textLable = new esri.Graphic(entCenterPoint, textSymbol, null, null);
            textLable.attributes = { name:es[i].name };
            
            graList.push(gra);
            textList.push(textLable);
            mymap.graphics.add(gra);
            mymap.graphics.add(textLable);
            console.log(graList);
        }


        for(var j=0; j<es[i].production.length; j++){
            var areaPolygom = {
                "geometry":{
                    "rings":[ es[i].production[j].points ],
                    "spatialReference": { "wkid": 102100 }
                },
                "attributes":{
                    "name":'production'
                },
                "symbol":{
                    "color":[0,187,255,64],
                    "outline":{
                        "color":[0,187,255,255],
                        "width":1,
                        "type":"esriSLS",
                        "style":"esriSLSSolid"
                    },
                    "type":"esriSFS",
                    "style":"esriSFSSolid"
                }
            };
            
            var gra = new esri.Graphic(areaPolygom);
            gra.hide();
            mymap.graphics.add(gra);
        }

        for(var k=0; k<es[i].storage.length; k++){
            var areaPolygom = {
                "geometry":{
                    "rings":[ es[i].storage[k].points ],
                    "spatialReference": { "wkid": 102100 }
                },
                "attributes":{
                    "name":'storage'
                },
                "symbol":{
                    "color":[145,197,53,64],
                    "outline":{
                        "color":[145,197,53,255],
                        "width":1,
                        "type":"esriSLS",
                        "style":"esriSLSSolid"
                    },
                    "type":"esriSFS",
                    "style":"esriSFSSolid"
                }
            };

            var gra = new esri.Graphic(areaPolygom);
            gra.hide();
            mymap.graphics.add(gra);
        }
    }

}

function loadParkRange(){
    if(!geo_points){
        return false;
    }
    var enter_range = eval(geo_points);
    var solidColor = [0,139,139],
        fillColor = [0, 238, 0,255],
        fill_color = [255,255,255,0];
    //newcontent += '<td '+'name ='+members[i][0] +'onclick="highLight();"'+'>' + members[i][0] + '</td>';
    var rangePolygon = {
        "geometry":{
            "rings":[enter_range],
            "spatialReference": { "wkid": 102100 }
        },
        "symbol":{
            "color":fill_color,
            "outline":{
                "color":fillColor,
                "width":4,
                "type":"esriSLS",
                "style":"esriSLSSolid"
            },
            "type":"esriSFS",
            "style":"esriSFSSolid"
        }
    };

    var gra = new esri.Graphic(rangePolygon);
    mymap.graphics.add(gra);
}

